
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        map<string, vector<string>> _map;
        for (string &str : strs)
        {
            string key = str;
            sort(key.begin(), key.end());
            _map[key].emplace_back(str);
        }
        vector<vector<string>> ans;
        for (auto it = _map.begin(); it != _map.end(); ++it)
        {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

int main()
{
    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    Solution solution;
    vector<vector<string>> ret = solution.groupAnagrams(strs);
    for (vector<vector<string>>::iterator iter = ret.begin(); iter != ret.end(); iter++)
    {
        for (vector<string>::iterator iter2 = iter->begin(); iter2 != iter->end(); iter2++)
        {
            cout << *iter2 << " ";
        }
        cout << endl;
    }
}
